﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.CodeDom;
using System.ComponentModel;
using System.Web.UI;
/// <summary>
/// Summary description for ResourcesExpressionBuilder
/// </summary>
public class ResourcesExpressionBuilder : System.Web.Compilation.ExpressionBuilder
{
	public ResourcesExpressionBuilder()
	{
	}
    public static object GetEvalData(string expression, Type target, string entry)
    {
        string culture = Utils.GetCulture(System.Web.HttpContext.Current.Request, System.Web.HttpContext.Current.Session);
        if (string.IsNullOrEmpty(culture))
        {
            culture = "vi-VN";
        }

        // get a reference to the assembly that contains my resource strings.  NOTE: my         
        // assembly name is: MyResources.dll        
        System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("App_GlobalResources");
        // Load the type from the assembly that contains my resource strings.  NOTE: my         
        // type is in the MyResources namespace and is called MyResourceObjectName.        
        Type type = assembly.GetType("Resources.General");
        // create a new instance of the resource manager class using the type.        
        System.Resources.ResourceManager resMgr = new System.Resources.ResourceManager(type);
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
        // get the resource string which is just the string name specified in the expression.     
        string str = resMgr.GetString(expression, System.Threading.Thread.CurrentThread.CurrentCulture);
        return str;
    }
    // This method is called when an expression is encountered by the parser.  It calls our custom method    
    // to evaluate the expression.   
    public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
    {
        return GetEvalData(entry.Expression, target.GetType(), entry.Name);
    }
    // This method parses the expression encountered in the '<%$ MyExpression goes here %>' property.    
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
    {
        Type type1 = entry.DeclaringType;
        PropertyDescriptor descriptor1 = TypeDescriptor.GetProperties(type1)[entry.PropertyInfo.Name];
        CodeExpression[] expressionArray1 = new CodeExpression[3];
        expressionArray1[0] = new CodePrimitiveExpression(entry.Expression.Trim());
        expressionArray1[1] = new CodeTypeOfExpression(type1);
        expressionArray1[2] = new CodePrimitiveExpression(entry.Name);
        return new CodeCastExpression(descriptor1.PropertyType, new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(base.GetType()), "GetEvalData", expressionArray1));
    }
    // Specifies that this class supports the ability to evaluate an expression.    
    public override bool SupportsEvaluate
    {
        get
        {
            return true;
        }
    }
}